package com.sgcc.ln.pmos.e_ln_mall.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SpanUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.sgcc.ln.pmos.common.bean.ComparisonDetailContentBean;
import com.sgcc.ln.pmos.common.bean.EventCenter;
import com.sgcc.ln.pmos.common.bean.PackageInfoMonthBean;
import com.sgcc.ln.pmos.common.utils.ArguConstant;
import com.sgcc.ln.pmos.common.utils.DateUtils;
import com.sgcc.ln.pmos.common.utils.DialogUtil;
import com.sgcc.pmos.nativesdk.R;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class ContentAdapter extends BaseQuickAdapter<ComparisonDetailContentBean.ContentBean, BaseViewHolder> {

    private int maxHeight = 200;//最大高度
    private OnTextMeasuredHeightListener onTextMeasuredHeightListener;
    private List<ComparisonDetailContentBean.ContentBean> contentBeanList;
    private int width;

    public void setOnTextMeasuredHeightListener(OnTextMeasuredHeightListener onTextMeasuredHeightListener) {
        this.onTextMeasuredHeightListener = onTextMeasuredHeightListener;
    }

    public ContentAdapter(List<ComparisonDetailContentBean.ContentBean> contentBeanList,int width) {
        super(R.layout.item_comparison_content, contentBeanList);
        this.contentBeanList = contentBeanList;
        this.width =width;
    }


    @Override
    protected void convert(BaseViewHolder holder, ComparisonDetailContentBean.ContentBean contentBean) {
        if (width > 0) {
            LinearLayout ll_content_itemLayout = holder.getView(R.id.ll_content_itemLayout);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) ll_content_itemLayout.getLayoutParams();
            layoutParams.width = width;
            ll_content_itemLayout.setLayoutParams(layoutParams);
        }
        int flag = contentBean.getFlag();
        TextView tvTitle = holder.getView(R.id.tvTitle);
        ImageView imgIcon = holder.getView(R.id.imgIcon);
        TextView selectMonth = holder.getView(R.id.selectMonth);
        tvTitle.setVisibility(View.INVISIBLE);
        imgIcon.setVisibility(View.INVISIBLE);
        selectMonth.setVisibility(View.INVISIBLE);

        if (flag == -1) {//-1 普通text  1 本地图片 2 套餐数据  9 选择套餐月份 下拉选择
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setTextColor(Color.parseColor("#333333"));
            tvTitle.setText(contentBean.getText());
        } else if (flag == 1) {
            imgIcon.setVisibility(View.VISIBLE);
            imgIcon.setImageResource(contentBean.getImgRes());

        } else if (flag == 2) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setTextColor(Color.parseColor("#333333"));
            ComparisonDetailContentBean.MealBean mealBean = contentBean.getMealBean();
            if (null != mealBean) {
                SpanUtils with = SpanUtils.with(tvTitle);
                with.append(mealBean.getText1());
                if (null != mealBean.getPriceCurveList() && mealBean.getPriceCurveList().size() > 1 && mealBean.getText1().equals("约定价格")) {
                    with.setClickSpan(Color.parseColor("#0092FF"), true, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            DialogUtil.showLineChartDialog(getContext(), mealBean.getPriceCurveList());
                        }
                    });
                }
                with.setForegroundColor(Color.parseColor(mealBean.getText1().equals("--") ? "#333333" : "#0092FF"));
                if (!TextUtils.isEmpty(mealBean.getText2())) {
                    with.append("\n").
                            append(mealBean.getText2()).setForegroundColor(Color.parseColor("#333333"));
                }
                with.create();
            }

        } else if (flag == 9) {
            selectMonth.setVisibility(View.VISIBLE);
            selectMonth.setTextColor(Color.parseColor("#333333"));
            List<PackageInfoMonthBean.Bean> monthDataList = contentBean.getMonthDataList();
            if (null != monthDataList && monthDataList.size() > 0) {
                String[] strings = new String[monthDataList.size()];
                for (int i = 0; i < monthDataList.size(); i++) {
                    strings[i] = DateUtils.getStrng_to_Times_ymd_3(monthDataList.get(i).getMonthDate());
                }
                selectMonth.setText(strings[contentBean.getSelectIndex()]);
                selectMonth.setOnClickListener(view ->
                        new XPopup.Builder(getContext())
                                .hasShadowBg(false)
                                .atView(view)
                                .asAttachList(strings,
                                        null,
                                        (position, text) -> {
                                            selectMonth.setText(text);
                                            String index = getItemPosition(contentBean) + "," + position;
                                            EventBus.getDefault().post(new EventCenter<>(ArguConstant.MONTH_REFRESH_CONTRAST, index));
//                                    schemeCombVo.setMarketFeeType(data.get(position).getConfigId());
                                        }, 0, 0, Gravity.LEFT).show());


            }


        }


        tvTitle.post(() -> {//获取最大高度
            maxHeight = Math.max(maxHeight, tvTitle.getMeasuredHeight());

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) tvTitle.getLayoutParams();
            layoutParams.height = maxHeight;
            tvTitle.setLayoutParams(layoutParams);
            if (getItemPosition(contentBean) == contentBeanList.size() - 1 && null != onTextMeasuredHeightListener)
                onTextMeasuredHeightListener.height(maxHeight);
        });
    }


    public interface OnTextMeasuredHeightListener {

        void height(int maxHeight);

    }
}

